package com.zhangmeng.tools.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-03-16 17:00
 */
@Slf4j
public class TelephoneController {

    public static final ObservableList<String> list1 = FXCollections.observableArrayList();
    public static final ObservableList<String> list2 = FXCollections.observableArrayList();
    public static final ObservableList<String> list3 = FXCollections.observableArrayList();

    public static final ObservableList<CheckBox> res_list = FXCollections.observableArrayList();
    public static final ObservableList<CheckBox> checkbox_list = FXCollections.observableArrayList();

    public static final ObservableList<String> show_list = FXCollections.observableArrayList();

    @FXML
    public AnchorPane root;

    @FXML
    public TextArea result_show;

    @FXML
    public TextField telephone;

    @FXML
    public Button query;

    @FXML
    private Button gen;

    @FXML
    private TextField num;

    @FXML
    public void initialize() {
        num.setText("100");
        init();
        init_checkbox(148.0, 168.0, list1);
        init_checkbox(238.0, 258.0, list2);
        init_checkbox(322.0, 342.0, list3);

        show_list.addListener((ListChangeListener<String>) c -> {
            while (c.next()){
                if (c.wasAdded()) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (String s : show_list) {
                        stringBuilder.append(s);
                    }
                    receive(stringBuilder.toString());
                }
            }
        });

        gen.setOnAction(actionEvent -> {
            show_list.clear();
            check_res();
            int size = Integer.parseInt(num.getText());
            for (int i = 0; i < size; i++) {

                CheckBox checkBox = null;
                int index;
                if (res_list.size() == 0) {
                    index = RandomUtil.randomInt(0, checkbox_list.size() - 1);
                    checkBox = checkbox_list.get(index);
                } else {
                    if (res_list.size() == 1 ){
                        index = 0 ;
                    }else {
                        index = RandomUtil.randomInt(0, res_list.size() - 1);
                    }
                    checkBox = res_list.get(index);
                }

                String res = checkBox.getText() + random_8();
                show_list.add(res);
                show_list.add(System.lineSeparator());
            }
        });
        query.setOnAction(actionEvent -> {
            Map<String,Object> map = new HashMap<>();
            map.put("phone",telephone.getText().substring(0,7));
            String map_res = HttpUtil.post("https://api.uutool.cn/phone/location_batch", map);
            R r = JSON.parseObject(map_res, R.class);
            R.Data.Rows rows = r.getData().getRows().get(0);
            String s = rows.getProvince() + "省  " + rows.city + "市  " + "区域编码：" +rows.area_code + "邮政编码：" + rows.getZip_code();
            result_show.setText(s);
        });
    }

    public  void receive(String msg) {
        Platform.runLater(() -> {
            this.result_show.setText(msg);
        });

    }

    public static class R {
        private int status;

        private Data data;

        private String req_id;

        public void setStatus(int status){
            this.status = status;
        }
        public int getStatus(){
            return this.status;
        }
        public void setData(Data data){
            this.data = data;
        }
        public Data getData(){
            return this.data;
        }
        public void setReq_id(String req_id){
            this.req_id = req_id;
        }
        public String getReq_id(){
            return this.req_id;
        }

        public static class Data {
            private int count;

            private List<Rows> rows ;

            public void setCount(int count){
                this.count = count;
            }
            public int getCount(){
                return this.count;
            }
            public void setRows(List<Rows> rows){
                this.rows = rows;
            }
            public List<Rows> getRows(){
                return this.rows;
            }

            public static class Rows {
                private String phone;

                private String isp;

                private String province;

                private String city;

                private String zip_code;

                private String area_code;

                private String segment;

                public void setPhone(String phone){
                    this.phone = phone;
                }
                public String getPhone(){
                    return this.phone;
                }
                public void setIsp(String isp){
                    this.isp = isp;
                }
                public String getIsp(){
                    return this.isp;
                }
                public void setProvince(String province){
                    this.province = province;
                }
                public String getProvince(){
                    return this.province;
                }
                public void setCity(String city){
                    this.city = city;
                }
                public String getCity(){
                    return this.city;
                }
                public void setZip_code(String zip_code){
                    this.zip_code = zip_code;
                }
                public String getZip_code(){
                    return this.zip_code;
                }
                public void setArea_code(String area_code){
                    this.area_code = area_code;
                }
                public String getArea_code(){
                    return this.area_code;
                }
                public void setSegment(String segment){
                    this.segment = segment;
                }
                public String getSegment(){
                    return this.segment;
                }

            }
        }
    }


    public void check_res() {
        res_list.clear();
        for (CheckBox checkBox : checkbox_list) {
            if (checkBox.isSelected()) {
                res_list.add(checkBox);
            }
        }
    }

    public void init_checkbox(double one, double two, ObservableList<String> list) {
        double i = 100;
        double j = 100;
        for (String t1 : list) {

            CheckBox checkBox = new CheckBox(t1);
            root.getChildren().add(checkBox);
            if (i <= 1000) {
                AnchorPane.setTopAnchor(checkBox, one);
                AnchorPane.setLeftAnchor(checkBox, i);
                i += 100;
            } else {
                AnchorPane.setTopAnchor(checkBox, two);
                AnchorPane.setLeftAnchor(checkBox, j);
                j += 100;
            }

            checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean t1) {
                    if (t1) {

                    }
                }
            });
            checkbox_list.add(checkBox);
        }
    }

    public void init() {
        list1.add("134");
        list1.add("135");
        list1.add("136");
        list1.add("137");
        list1.add("138");
        list1.add("139");
        list1.add("147");
        list1.add("150");
        list1.add("151");
        list1.add("152");
        list1.add("157");
        list1.add("158");
        list1.add("159");
        list1.add("165");
        list1.add("172");
        list1.add("178");
        list1.add("182");
        list1.add("183");
        list1.add("184");
        list1.add("187");
        list1.add("188");
        list1.add("198");

        list2.add("130");
        list2.add("131");
        list2.add("132");
        list2.add("145");
        list2.add("155");
        list2.add("156");
        list2.add("166");
        list2.add("171");
        list2.add("175");
        list2.add("176");
        list2.add("185");
        list2.add("186");

        list3.add("133");
        list3.add("149");
        list3.add("153");
        list3.add("173");
        list3.add("177");
        list3.add("180");
        list3.add("181");
        list3.add("189");
        list3.add("199");
    }

    public String random_8() {
        int i = RandomUtil.randomInt(10000000, 99999999);
        return String.valueOf(i);
    }
}
